#!/bin/python

# --------------------------------------
# VIDEO BUDDHA 2 - database class
# --------------------------------------
# Ranames files based on a specific regex pattern in 
#  show_list.db and based on a specific list of file 
#  extensions.
# --------------------------------------

from sqlite3 import *
from sqlite3 import dbapi2 as sqlite

class Database:
	
	def __init__(self):
		# Establish a DB connection
		self.conn = connect('definitions.db')
		self.curs = self.conn.cursor()
		
		# Create the db if it doesn't exist
		self.create_tables()
			
	def __del__(self):
		self.curs.close()
		self.conn.close()
		
	def close(self):
		self.__del__()
		
	def create_tables(self):
		self.curs.execute("create table IF NOT EXISTS definitions (id integer primary key, old_name text, new_name text)")
		self.conn.commit()
			
	def insert(self, oldName, newName):
		self.curs.execute("insert into definitions values (?, ?, ?)", (None, oldName, newName))
		self.conn.commit()
	
	def delete(self, id):
		self.curs.execute("delete from definitions where id = ?", id)
		self.conn.commit()
		
	def update(self, id, oldName, newName):
		self.curs.execute("update definitions set old_name = ?, new_name = ? where id = ?", (oldName, newName, id))
		self.conn.commit()
			
	def retrieve(self):
		data = self.curs.execute("select id, old_name, new_name from definitions")
		return data
		
	def retrieveId(self, id):
		data = self.curs.execute("select * from definitions where id = ?", [id])
		return data